//
//  MyBagCell.swift
//  Eternity
//
//  Created by cll12_12 on 2016/12/5.
//  Copyright © 2016年 ZhengWei. All rights reserved.
//

import UIKit

protocol MyBagCellDelegate: NSObjectProtocol {
    func selectProductAction(sender: UIButton)
}

class MyBagCell: MGSwipeTableCell {
    
    var isSelectedArr: [Bool]?
    var _indexPath: IndexPath?
    var indexPath: IndexPath? {
        set {
            selectedButton.tag = 100 + (newValue?.row)!
        } get {
            return _indexPath
        }
    }
    
    var bagDelegate: MyBagCellDelegate!
    
    // MARK: - life cycle
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initBaseLayout()
        layoutPageSubviews()
        self.backgroundColor = UIColor.white
        self.selectionStyle = .none
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - event response
    func selectProductAction(sender: UIButton) {
        
        self.bagDelegate.selectProductAction(sender: sender)
    }
    
    func addAction(sender: UIButton) {
        if accountTextField.text != "" {
            var account = Int(accountTextField.text!)
            account! += 1
            if account! > 1 {
                subButton.isEnabled = true
            }
            accountTextField.text = String(format: "%d", account!)
        } else {
            accountTextField.text = "1"
        }
        
    }
    
    func subAction(sender: UIButton) {
        
        var account = Int(accountTextField.text!)
        account! -= 1
        if account == 1 {
            sender.isEnabled = false
        }
        accountTextField.text = String(format: "%d", account!)
    }
    
    // MARK: - private method
    func initBaseLayout() -> Void {
        self.contentView.addSubview(selectedButton)
        self.contentView.addSubview(iconImageView)
        self.contentView.addSubview(productLabel)
        self.contentView.addSubview(netContentLabel)
        self.contentView.addSubview(colorNumLabel)
        self.contentView.addSubview(lowStockLabel)
        self.contentView.addSubview(priceLabel)
        self.contentView.addSubview(addButton)
        self.contentView.addSubview(accountTextField)
        self.contentView.addSubview(subButton)
    }
    
    func layoutPageSubviews() -> Void {
        selectedButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.left.equalTo(self).offset(5)
            make.width.height.equalTo(32)
        }
        
        iconImageView.snp.makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.left.equalTo(selectedButton.snp.right).offset(0)
            make.width.height.equalTo(100)
        }
        
        productLabel.snp.makeConstraints { (make) in
            make.left.equalTo(iconImageView.snp.right).offset(14)
            make.top.equalTo(self).offset(34)
            make.width.equalTo(98)
            make.height.equalTo(12)
        }
        
        netContentLabel.snp.makeConstraints { (make) in
            make.left.equalTo(iconImageView.snp.right).offset(14)
            make.top.equalTo(productLabel.snp.bottom).offset(10)
            make.width.equalTo(83)
            make.height.equalTo(12)
        }
        
        colorNumLabel.snp.makeConstraints { (make) in
            make.left.equalTo(netContentLabel.snp.right).offset(8)
            make.top.equalTo(productLabel.snp.bottom).offset(10)
            make.width.equalTo(65)
            make.height.equalTo(12)
        }
        
        lowStockLabel.snp.makeConstraints { (make) in
            make.left.equalTo(iconImageView.snp.right).offset(14)
            make.top.equalTo(netContentLabel.snp.bottom).offset(8)
            make.width.equalTo(48)
            make.height.equalTo(12)
        }
        
        priceLabel.snp.makeConstraints { (make) in
            make.left.equalTo(iconImageView.snp.right).offset(14)
            make.top.equalTo(netContentLabel.snp.bottom).offset(33)
            make.width.equalTo(60)
            make.height.equalTo(14)
        }
        
        addButton.snp.makeConstraints { (make) in
            make.right.equalTo(self).offset(-14)
            make.bottom.equalTo(self).offset(-18)
            make.width.equalTo(24)
            make.height.equalTo(24)
        }
        
        accountTextField.snp.makeConstraints { (make) in
            make.right.equalTo(addButton.snp.left).offset(0)
            make.bottom.equalTo(self).offset(-18)
            make.width.equalTo(25)
            make.height.equalTo(24)
        }
        
        subButton.snp.makeConstraints { (make) in
            make.right.equalTo(accountTextField.snp.left).offset(0)
            make.bottom.equalTo(self).offset(-18)
            make.width.equalTo(24)
            make.height.equalTo(24)
        }
        
    } 
    
    func configUI(isSelectedArr: [Bool], indexPath: IndexPath) {
        iconImageView.sd_setImage(with: URL(string: "http://img15.3lian.com/2015/h1/57/d/61.jpg"), placeholderImage: UIImage(named: "def_162x162"))
        productLabel.text = "苦橙收敛调理液"
        netContentLabel.text = "净含量：200ml"
        colorNumLabel.text = "色号：#456"
        lowStockLabel.text = "库存紧张"
        priceLabel.text = "￥335.00"
        accountTextField.text = "1"
        if accountTextField.text == "1" {
            subButton.isEnabled = false
        }
        selectedButton.isSelected = isSelectedArr[indexPath.row]
    }
    
    // MARK: - setters and getters
    var _selectedButton: UIButton!
    var selectedButton: UIButton {
        if _selectedButton == nil {
            _selectedButton = UIButton()
            _selectedButton.setBackgroundImage(UIImage(named: "icon_select_n"), for: .normal)
            _selectedButton.setBackgroundImage(UIImage(named: "icon_select_h"), for: .highlighted)
            _selectedButton.setBackgroundImage(UIImage(named: "icon_select_s"), for: .selected)
            _selectedButton.addTarget(self, action: #selector(MyBagCell.selectProductAction(sender:)), for: .touchUpInside) 
        }
        return _selectedButton
    }
    
    var _iconImageView: UIImageView!
    var iconImageView: UIImageView {
        if _iconImageView == nil {
            _iconImageView = UIImageView()
        }
        return _iconImageView
    }
    
    var _productLabel: UILabel!
    var productLabel: UILabel {
        if _productLabel == nil {
            _productLabel = UILabel()
            _productLabel.font = UIFont(name: "PingFangSC-Regular", size: 14)
            _productLabel.textColor = UIColor(hexString: "#252525")
        }
        return _productLabel
    }
    
    var _netContentLabel: UILabel!
    var netContentLabel: UILabel {
        if _netContentLabel == nil {
            _netContentLabel = UILabel()
            _netContentLabel.font = UIFont(name: "PingFangSC-Light", size: 12)
            _netContentLabel.textColor = UIColor(hexString: "#999999")
        }
        return _netContentLabel
    }
    
    var _colorNumLabel: UILabel!
    var colorNumLabel: UILabel {
        if _colorNumLabel == nil {
            _colorNumLabel = UILabel()
            _colorNumLabel.font = UIFont(name: "PingFangSC-Light", size: 12)
            _colorNumLabel.textColor = UIColor(hexString: "#999999")
//            _colorNumLabel.isHidden = true
        }
        return _colorNumLabel
    }
    
    var _lowStockLabel: UILabel!
    var lowStockLabel: UILabel {
        if _lowStockLabel == nil {
            _lowStockLabel = UILabel()
            _lowStockLabel.font = UIFont(name: "PingFangSC-Light", size: 12)
            _lowStockLabel.textColor = UIColor(hexString: "#B7A18A")
            _lowStockLabel.text = "库存紧张"
//            _lowStockLabel.isHidden = true
        }
        return _lowStockLabel
    }
    
    var _priceLabel: UILabel!
    var priceLabel: UILabel {
        if _priceLabel == nil {
            _priceLabel = UILabel()
            _priceLabel.font = UIFont(name: "PingFang-SC-Regular", size: 14)
            _priceLabel.textColor = UIColor(hexString: "#B50834")
        }
        return _priceLabel
    }

    var _subButton: UIButton!
    var subButton: UIButton {
        if _subButton == nil {
            _subButton = UIButton()
            _subButton.setBackgroundImage(UIImage(named: "icon_uisteppersubtract_n"), for: .normal)
            _subButton.setBackgroundImage(UIImage(named: "icon_uisteppersubtract_h"), for: .highlighted)
            _subButton.setBackgroundImage(UIImage(named: "icon_uisteppersubtract_d"), for: .disabled)
            _subButton.addTarget(self, action: #selector(MyBagCell.subAction(sender:)), for: .touchUpInside)
        }
        return _subButton
    }
    
    var _addButton: UIButton!
    var addButton: UIButton {
        if _addButton == nil {
            _addButton = UIButton()
            _addButton.setBackgroundImage(UIImage(named: "icon_uistepperadd_n"), for: .normal)
            _addButton.setBackgroundImage(UIImage(named: "icon_uistepperadd_h"), for: .highlighted)
            _addButton.setBackgroundImage(UIImage(named: "icon_uistepperadd_d"), for: .disabled)
            _addButton.addTarget(self, action: #selector(MyBagCell.addAction(sender:)), for: .touchUpInside)
        }
        return _addButton
    }
    
    var _accountTextField: UITextField!
    var accountTextField: UITextField {
        if _accountTextField == nil {
            _accountTextField = UITextField()
            _accountTextField.font = UIFont(name: ".AppleSystemUIFont", size: 12)
            _accountTextField.textColor = UIColor(hexString: "#252525")
            _accountTextField.textAlignment = .center
            _accountTextField.background = UIImage(named: "icon_uistepper_n")
            _accountTextField.delegate = self
        }
        return _accountTextField
    }
}

extension MyBagCell: UITextFieldDelegate {
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        let lastStr = textField.text
        
        if lastStr?.characters.count == 2 {
            if string == "" && lastStr!.substring(to: lastStr!.index(lastStr!.startIndex, offsetBy: 1)) == "1" {
                subButton.isEnabled = false
            } else {
                subButton.isEnabled = true
            }
        } else if lastStr?.characters.count == 1 {
            if string == "" {
                subButton.isEnabled = false
            } else {
                subButton.isEnabled = true
            }
        } else if lastStr?.characters.count == 0 {
            if string == "1" || string == "0" {
                subButton.isEnabled = false
            } else {
                subButton.isEnabled = true
            }
        } else {
            subButton.isEnabled = true
        }

        if (lastStr?.characters.count)! > 0 {
            if !string.isNumber() && string != "" {
                return false
            }
        }
        return true
    }
}
